package bcxd.firstline.focalcompany.machining;

import java.sql.Connection;

import jt.classic.system.context.ISystemContext;
import jt.classic.system.database.SystemDataSource;
import jt.classic.system.user.IUser;

import org.limp.basework.MachiningException;
import org.limp.basework.SimpleBean;
import org.limp.basework.SimpleBean4Table;
import org.limp.basework.SimpleBeanMachining;
import org.limp.basework.Table;
import org.limp.basework.impl.CommonBean4HSQ;
import org.limp.basework.impl.SimpleBean4TableImpl;
import org.limp.basework.tools.BaseworkUtil;
import org.limp.mine.DateTrimmer;

import bcxd.firstline.user.AppUser;
import bcxd.firstline.user.manager.AppUserManager;

/**
 * 核心企业入驻申请数据加工
 * 
 * @author mido
 * 
 */
public class FocalCompanyApplyMachining implements SimpleBeanMachining {

	public SimpleBean machining(SimpleBean arg0, Table arg1,
			CommonBean4HSQ arg2, Connection arg3, int arg4)
			throws MachiningException {
		// TODO Auto-generated method stub
		if (arg4 == 3) {

			if (ISystemContext.invalidSesstion(arg2.getRequest())) {
				throw new MachiningException("invalid session.");
			}
			// 更新会话用户对象信息
			IUser user = ISystemContext.getSessionUser(arg2.getRequest());

			SimpleBean4Table bean = new SimpleBean4TableImpl("bcxduser");

			bean.getResource().put("uid", user.id());
			bean.getResource().put("uname", arg2.getResource().get("fcname"));
			bean.getResource().put("ustatus", "1");

			BaseworkUtil util = new BaseworkUtil();
			try {
				util.manualUpdate1(bean, arg3);
			} catch (Exception ex) {
				throw new MachiningException("update application-user error.",
						ex);
			}

		} else if (arg4 == 4000) {
			// 入驻资料更新成功,重置会话
			IUser user = ISystemContext.getSessionUser(arg2.getRequest());
			//首次入驻更新资料信息,更新后重置会话
			if (user.iflag() == 0 || true) {
				Connection conn = null;
				try {
					SystemDataSource ds = SystemDataSource.getInstance();
					conn = ds.getConnection();

					AppUserManager manager = new AppUserManager(conn);

					IUser appUser = manager.getBcxdUserById(user.id());

					ISystemContext.resetSessionUser(arg2.getRequest(), appUser);
				} catch (Exception ex) {
					SystemDataSource.rollbackConnection(conn);
					throw new MachiningException("get application-user error.",
							ex);
				} finally {
					SystemDataSource.closeConnection(conn);
				}
			}
		}
		return null;
	}
}
